# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-TODAY OpenERP S.A. <http://www.openerp.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.addons.dm_base.utils import field_boolean_name_cn as boolean_name,field_selection_name as selection_name
from openerp.addons.dm_rubylong import get_rubylong_fields_xml,get_rublong_data_url
from openerp.osv import fields, osv
from datetime import datetime
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT

class pur_req(osv.osv):
    
    _inherit="pur.req"
    
    def _get_rubylong_xml_pur_req(self, cr, uid, ids, field_names, args, context=None):
        if isinstance(ids, (int, long)):
            ids = [ids]
        res = {}
        for order_id in ids:
            res[order_id] = ''
        data_xml = ''
        for order in self.browse(cr, uid, ids, context=context):
            #当前对象的xml
            data_xml = ''
            #header data
            order_fields = [
                        ('company_id.logo','company_logo'),
                        ('company_id.name','company_id_name'),
                        #采购申请
                        'name',
                        #请购人
                        ('user_id.name','user_id'),
                        #仓库
                        ('warehouse_id.name','warehouse_id'),
                        
                        ('id','order_id'), 
                        ]
            #生成PO完毕
            full_gen_po_name=""
            if order.full_gen_po:
                full_gen_po_name = "是"
            else:
                full_gen_po_name = "否"
            order_fields.append({'full_gen_po':full_gen_po_name})
            
            #请购日期
            date_request = order.date_request and (fields.datetime.context_timestamp(cr, uid,
                                                        timestamp=datetime.strptime(order.date_request, DEFAULT_SERVER_DATETIME_FORMAT),
                                                         context=context)) or ''
                
            order_fields.append({'date_request':date_request})
            data_xml += get_rubylong_fields_xml(order, 'header', order_fields)
            
            #detail data
            line_fields = [
                        #产品
                        ('product_id.default_code','default_code'),
                        ('product_id.name','product_id'),
                        #数量
                        'product_qty',
                        #单位
                        ('product_uom_id.name','product_uom_id'),
                        #需求日期
                        'date_required',
                        #原因和用途
                        'req_reason',
                        
#                        #采购数量
#                        ('product_uom.name','uom_po_qty'),
#                        #剩余数量
#                        'product_qty_remain',
#                        #库位
#                        'inv_qty',
#                        #库存单位
#                        ('inv_uom_id.name','inv_uom_id'),
                        
                        ('req_id.id','order_id'),   
                        ] 
            
            seq = 0
            for line in order.line_ids:
                seq += 1
                line_fields_new = line_fields + [{'order_line':seq}]
                data_xml += get_rubylong_fields_xml(line, 'detail', line_fields_new)

            #write data to file            
            res[order.id] = get_rublong_data_url(order,data_xml,cr.dbname)
            
        return res            

    _columns = {
            'rubylong_xml_file':fields.function(_get_rubylong_xml_pur_req, type='text', string='Rubylong xml'),
            }
    
#po_super.STATE_SELECTION = STATE_SELECTION_PO  